<template>
    <div style="border: 1px solid #ccc">
        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
        <Editor style="overflow-y: hidden" :style="{ height: height }" v-model="content" :defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" @onChange="onChange" />
    </div>
</template>
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { DomEditor } from "@wangeditor/editor";
import api from "@/api";
import store from "@/store";
export default {
    components: { Editor, Toolbar },
    model: {
        prop: "defaultContent", // 用来接收父组件传来的值
        event: "change", // 用来触发的事件
    },
    props: {
        defaultContent: {
            type: String,
            required: true,
            default: "",
        },
        height: {
            type: String,
            default: "25vh",
        },
    },
    data() {
        return {
            editor: null,
            toolbarConfig: {
                excludeKeys: ["|", "group-image", "insertVideo", "codeBlock"],
                insertKeys: {
                    index: 21,
                    keys: ["uploadImage"],
                },
            },
            editorConfig: {
                MENU_CONF: {
                    uploadImage: {
                        server: api.getUploadUrl(),

                        // form-data fieldName ，默认值 'wangeditor-uploaded-image'
                        fieldName: "file",

                        // 单个文件的最大体积限制，默认为 2M
                        maxFileSize: 10 * 1024 * 1024, // 1M

                        // 最多可上传几个文件，默认为 100
                        maxNumberOfFiles: 10,

                        // 选择文件时的类型限制，默认为 ['image/*'] 。如不想限制，则设置为 []
                        allowedFileTypes: ["image/*"],

                        // 自定义上传参数，例如传递验证的 token 等。参数会被添加到 formData 中，一起上传到服务端。
                        meta: {},

                        // 将 meta 拼接到 url 参数中，默认 false
                        metaWithUrl: false,

                        // 自定义增加 http  header
                        headers: {
                            Authorization: "Bearer " + store.getters.getToken,
                        },

                        // 跨域是否传递 cookie ，默认为 false
                        withCredentials: true,

                        // 超时时间，默认为 10 秒
                        timeout: 5 * 60 * 1000, // 5 秒

                        // 自定义插入图片
                        customInsert(res, insertFn) {
                            // res 即服务端的返回结果
                            insertFn(res.data.http_url, res.data.filename, "");
                        },
                    },
                },
            },
            mode: "simple",
            content: "",
        };
    },
    beforeDestroy‌() {
        const editor = this.editor;
        if (editor == null) return;
        editor.destroy(); // 组件销毁时，及时销毁编辑器
    },
    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
            this.content = this.defaultContent;
        },
        onChange() {
            this.$emit("change", this.content);
        },
        getToolbar() {
            const toolbar = DomEditor.getToolbar(this.editor);
            const curToolbarConfig = toolbar.getConfig();
            console.log(curToolbarConfig.toolbarKeys);
        },
    },
};
</script>
<style lang="less">
@import "@wangeditor/editor/dist/css/style.css";
</style>
